package bb.lanxing.device.sgdevcie.storage.adapter;

import static bb.lanxing.lib.devices.sgsettingentity.workout.WorkoutState.Synced;

import java.util.ArrayList;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import bb.lanxing.R;
import bb.lanxing.databinding.EmptyViewBinding;
import bb.lanxing.databinding.LayoutSgWorkoutManagerItemBinding;
import bb.lanxing.lib.devices.sgsettingentity.workout.Workout;
import bb.lanxing.lib.devices.sgsettingentity.workout.WorkoutState;
/* compiled from: BBWorkoutsManageAdapter.kt */

public final class BBWorkoutsManageAdapter extends BaseQuickAdapter<Workout, BaseViewHolder> {
	private boolean isEditMode = true;
	private static final String TAG = "BBWorkoutsManageAdapter";

	public BBWorkoutsManageAdapter() {
		super(R.layout.layout_sg_workout_manager_item, null);
	}

	public final boolean isEditMode() {
		return this.isEditMode;
	}

	public final void setEditMode(boolean z) {
		this.isEditMode = z;
		notifyDataSetChanged();
	}

	@NonNull @Override public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {//RecyclerView.ViewHolder
		LayoutInflater from = LayoutInflater.from(parent.getContext());
		if (viewType == EMPTY_VIEW) {
			Log.d(TAG, " the view is empty view");
			return new BBWorkoutsManageAdapter.WorkoutManagerEmptyViewHolder(DataBindingUtil.inflate(from, R.layout.empty_view, parent, false));
		} else {
			Log.d(TAG, " the view is not empty view");
			return new BBWorkoutsManageAdapter.LayoutWorkoutManagerItemHolder(DataBindingUtil.inflate(from, R.layout.layout_sg_workout_manager_item, parent, false));
		}
	}

	public void checkAll() {
		for (Workout workout : getData()) {
			workout.setChecked(true);
		}
		notifyDataSetChanged();
	}

	public void checkSynced() {
		for (Workout workout : getData()) {
			workout.setChecked(workout.getState() == Synced);
		}
		notifyDataSetChanged();
	}

	public void clearChecked() {
		for (Workout workout : getData()) {
			workout.setChecked(false);
		}
		notifyDataSetChanged();
	}

	public ArrayList<Workout> getCheckedItem() {
		ArrayList<Workout> arrayList = new ArrayList<>();
		for (Workout item : getData()) {
			if (item.getChecked()) {
				arrayList.add(item);
			}
		}
		return arrayList;
	}

	@Override protected void convert(@NonNull BaseViewHolder baseViewHolder, Workout item) {
		if (baseViewHolder instanceof LayoutWorkoutManagerItemHolder) {
			Log.d(TAG, " show layout item view");
			LayoutSgWorkoutManagerItemBinding dataBinding = ((LayoutWorkoutManagerItemHolder) baseViewHolder).getBinding();
			if (dataBinding != null) {

				dataBinding.llRoot.setOnClickListener(view -> {
					if (isEditMode) {
						item.setChecked(!item.getChecked());
						notifyItemChanged(getItemPosition(item));
					}
				});
				dataBinding.checkbox.setChecked(item.getChecked());
				dataBinding.workoutFileName.setText(String.valueOf(item.getTimestampInYHDHMS()));

				float fileSizef = item.getFileSize() / 1024f;
				String size = String.format("%.1f", fileSizef);
				//Log.d(TAG," size: " + size +"   fileSizef: " + fileSizef);
				dataBinding.workoutFileSize.setText(size + "K");
				if (item.getState().equals(WorkoutState.Synced)) {
					dataBinding.stateSynced.setVisibility(View.VISIBLE);
					dataBinding.stateUnsync.setVisibility(View.GONE);
					dataBinding.stateBroken.setVisibility(View.GONE);
				} else if (item.getState().equals(WorkoutState.UnSynced)) {
					dataBinding.stateUnsync.setVisibility(View.VISIBLE);
					dataBinding.stateSynced.setVisibility(View.GONE);
					dataBinding.stateBroken.setVisibility(View.GONE);
				} else if (item.getState().equals(WorkoutState.Broken)) {
					dataBinding.stateBroken.setVisibility(View.VISIBLE);
					dataBinding.stateSynced.setVisibility(View.GONE);
					dataBinding.stateUnsync.setVisibility(View.GONE);
				}
			}
		} else {
			Log.d(TAG, " show empty view");
		}
	}

	private static class WorkoutManagerEmptyViewHolder extends BaseViewHolder {
		private final EmptyViewBinding emptyViewBinding;

		private WorkoutManagerEmptyViewHolder(EmptyViewBinding binding) {
			super(binding.getRoot());
			emptyViewBinding = binding;
		}

		public EmptyViewBinding getEmptyViewBinding() {
			return emptyViewBinding;
		}
	}

	private static class LayoutWorkoutManagerItemHolder extends BaseViewHolder {
		private final LayoutSgWorkoutManagerItemBinding layoutWorkoutManagerItemBinding;

		private LayoutWorkoutManagerItemHolder(LayoutSgWorkoutManagerItemBinding binding) {
			super(binding.getRoot());
			layoutWorkoutManagerItemBinding = binding;
		}

		public LayoutSgWorkoutManagerItemBinding getBinding() {
			return layoutWorkoutManagerItemBinding;
		}
	}
}
